import type {Router} from 'vue-router';
import Config from "@/config";
import useUserStore from "@/store/modules/user";

/**
 * create route guard
 *
 * @param router router instance
 */
export function createAuthGuard(router: Router) {

    router.beforeEach(async (to, from, next) => {
        const userStore = useUserStore();
        // 白名单
        if (Config.whiteList.includes(to.path)) {
            next()
        }
        // 未登录跳转登录页
        if (!userStore.isLogin()) {
            next({path: '/login'})
        }
        next()
    })
}
